$(function() {
  var user = null,
    thisPage = null,
    defaultAddress = [],
    addressesArray = [],
    html = [];

  var confirmOrderPage = function() {
    this.init();
  };
  confirmOrderPage.prototype = {
    init: function() {
      this.initData();
      this.bindEvent();
    },
    initData: function() {
      user = getLocalStorage(STORAGE_MANAGER.user);
      if (!user || !user.id) {
        redirect("../account/login.html");
        return false;
      }
      this.getConfirmorder();
      this.getAddress();
    },
    bindEvent: function() {
      //点击后退
      $("#back").on("tap", function(){ 
        back();
      });
      
      //收货地址  
      this.changeAddress();
      
      //发票
      this.addInvoice();
      
      //选择支付配送
      this.chooseDeliver();
      
      //提交订单
      this.commitOrder();
    },
    //获取订单商品信息
    getConfirmorder:function(){
      
      this.loadContent();
      
      var requestData = {
        member_id       : user.id,
        share_member_id :0,
        address_id      :0,
        region_no       :'',
        platform_no     :'',
        coupon_no       :'',
        organize_id     :6010,
        express_flag    :1,
        shops           :[],
      };
      $.ajax({
      	type:"POST",
      	url: CONFIG_MANAGER.RequstUrl + "v1/cart/confirmorder",
      	data: requestData,
      	headers:{ token: user.token },
      	async:true
      }).done(function(data){        
        thisPage.loadContent(data);
      }).fail(function(data){
        var infor = JSON.parse(data);
        $.toast(infor.Message);
      });
      
    },
    //加载订单内容
    loadContent:function(){
      html=[];
      html.push('<div class="wlxqbg"><div class="wlxqnrbg"><div class="bg-fff mbtopbottom ddnybg">');
      html.push('<div class="ddnybbt mbbottom">');
      html.push('<p class="shxxbt1"><span>生活日用品商铺</span></p>');
      html.push('</div>');
      html.push('<div class="mbbottom  overflow">');
      html.push('<dl class="cpxqxztop  mtop0">');
      html.push('<dt><img src="../../images/cpxdtu1.jpg"/></dt>');
      html.push('<dd class="wdgzwz wdgzwz-20">');
      html.push('<p class="floatr  money2">');
      html.push('<span class="jinbi"><b><img src="../../images/jbtub.jpg"/></b>70</span>+');
      html.push('<span class="jinbi"></span>');
      html.push('<span class="money">￥24</span>');
      html.push('</p>');
      html.push('玫瑰花嫩白精油手工皂夏季专 嫩白祛除黄玫瑰花嫩白精油手工皂夏季专 嫩白祛除黄玫瑰花嫩白精油手工皂夏季专 嫩白祛除');
      html.push('</dd>');
      html.push('<dd class="huisezi">');
      html.push('<span class="x2">x2</span>净含量：120ml');
      html.push('</dd>');
      html.push('</dl>');
      html.push('</div>');
      html.push('<div class="mbbottom  overflow">');
      html.push('<dl class="cpxqxztop  mtop0">');
      html.push('<dt><img src="../../images/cpxdtu1.jpg"/></dt>');
      html.push('<dd class="wdgzwz wdgzwz-30">');
      html.push('<p class="floatr">');
      html.push('<span class="jinbi"></span>');
      html.push('<span class="money money2">￥24</span>');
      html.push('</p>');
      html.push('玫瑰花嫩白精油手工皂夏季专 嫩白祛除黄玫瑰花嫩白精油手工皂夏季专 嫩白祛除黄玫瑰花嫩白精油手工皂夏季专 嫩白祛');
      html.push('</dd>');
      html.push('<dd class="huisezi"><span class="x2">x2</span>净含量：120ml</dd>');
      html.push('</dl>');
      html.push('</div>');
      html.push('<div class="wlxqnrbg">');
      html.push('<p class="wlxqnr  mbbottom">');
      html.push('<span><label id="deliver">在线支付快递</label><i class="iconfont icon-jiantou-right"></i></span>支付配送');
      html.push('</p>');
      html.push('<p class="wlxqnr  mbbottom">');
      html.push('<span><label id="invoice">不开发票</label><i class="iconfont icon-jiantou-right"></i></span>开具发票');
      html.push('</p>');
      html.push('</div>');
      html.push('<div class="gjjsl mbbottom">');
      html.push('<p class="floatr">共计3件商品合计<span class="jinbi"><b>');
      html.push('<img src="../../images/jbtub.jpg"/></b>70</span>+');
      html.push('<span class="jinbi"></span>');
      html.push('<span class="money">￥24</span>');
      html.push('</p>');
      html.push('</div>');
      html.push('<div class="gmjliuyan">');
      html.push('<input type="text" placeholder="给卖家留言" class="gmjliuyanny"/>');
      html.push('</div>');
      html.push('</div></div></div>');
      
      $("#defaultAddress").after(html.join(''));
    },
    //获取用户的默认收货地址
    getAddress: function() {
      var i = 0;
      $.ajax({
        type: "GET",
        url: CONFIG_MANAGER.RequstUrl + "v1/member/address/" + user.id,
        async: true
      }).done(function(data) {
        var list = JSON.parse(data);
        data = list.data;
        addressesArray = data;
        while (data[i]) {
          //默认地址显示在页面上
          if(data[i].default_address){
            defaultAddress.push('<div class="shdzk mbtopbottom">');
            defaultAddress.push('<p class="dwei"><img src="../../images/ditutb.png"></p>');
            defaultAddress.push('<div class="shdzhi">');
            defaultAddress.push('<p class="sjrmc"><span>18788829010</span><b class="mrziti">[默认]</b>收件人：' + data[i].consignee + '</p>');
            defaultAddress.push('<p>' + data[i].region_text + ' ' + data[i].address + '</p>');
            //html.push('<p><span>发票：无</span>备注：请发圆通快递</p>');
            defaultAddress.push('<p  class="sjrmc"><span><b class="ybzt"></b><i class="iconfont icon-jiantou2"></i></span><span id="selectAddress">选择其他收货地址</span></p>');
            defaultAddress.push('</div>');
            defaultAddress.push('</div>');
          }
          //所有地址隐藏起来
          html.push('<div class="shdzbg" name="address" tip="'+ data[i].id +'">');
          html.push('<div class="shdzk'+ (data[i].default_address? "":"1") +'">');
          //html.push('<p class="dwei"><img src="../../images/ditutb'+ (data[i].default_address? "":"1") +'.png"></p>');
          html.push('<div style="width:100%" class="shdzhi">');
          html.push('<p class="sjrmc"><span>15381105360</span>'+ (data[i].default_address ? '<b class="mrziti">[默认]</b>':'') +'收件人：' + data[i].consignee + '</p>');
          html.push('<p>' + data[i].region_text + ' ' + data[i].address + '</p>');
          html.push('</div>');
          html.push('</div>');
          html.push('</div>');          
          i++;
        }
        $("#defaultAddress").html(defaultAddress.join(''));
        $("#addressItem").html(html.join(''));
      }).fail(function(data) {
        var infor = JSON.parse(data);
        $.alert(infor.Message);
      });
    },
    changeAddress:function(){
      //选择其他地址
      $("#defaultAddress").on("click", "#selectAddress", function(){
        $("#pop_back").show();
        if( $("#addresses_pop").hasClass("pop_init") ){
          $("#addresses_pop").removeClass("pop_init ");
        }
        $("#addresses_pop").removeClass("pop_hid").addClass("pop_show");
      });
      
      //取消选择
      $("#cancelSelect").on("click", function(){
        $("#pop_back").hide();
        $("#addresses_pop").removeClass("pop_show").addClass("pop_hid");
      });
      
      //从地址列表中选择一个，填充当前收货地址
      $("#addresses_pop").on("tap", "div[name='address']", function(){
        var tip = $(this).attr("tip"),j=0,defaultAddress=[];
        while (addressesArray[j]){
        	if( tip == addressesArray[j].id ){        	         	  
            defaultAddress.push('<div class="shdzk'+ (addressesArray[j].default_address ? '' : '1') +' mbtopbottom">');
            defaultAddress.push('<p class="dwei"><img src="../../images/ditutb'+ (addressesArray[j].default_address ? '' : '1') +'.png"></p>');
            defaultAddress.push('<div class="shdzhi">');
            defaultAddress.push('<p class="sjrmc"><span>18788829010</span>'+ (addressesArray[j].default_address ? '<b class="mrziti">[默认]</b>' : '') +'收件人：' + addressesArray[j].consignee + '</p>');
            defaultAddress.push('<p>' + addressesArray[j].region_text + ' ' + addressesArray[j].address + '</p>');
            //html.push('<p><span>发票：无</span>备注：请发圆通快递</p>');
            defaultAddress.push('<p  class="sjrmc"><span><b class="ybzt"></b><i class="iconfont icon-jiantou2"></i></span><span id="selectAddress">选择其他收货地址</span></p>');
            defaultAddress.push('</div>');
            defaultAddress.push('</div>');
            
            $("#defaultAddress").html(defaultAddress.join(''));
            $("#pop_back").hide();
            $("#addresses_pop").removeClass("pop_show").addClass("pop_hid");
        	  break;
        	}
        	j++;
        }
      });
    },
    //开发票
    addInvoice:function(){
      //选择其他发票
      $("#invoice").on("tap", function(){
        $("#pop_back").show();
        if( $("#invoices_pop").hasClass("pop_init") ){
          $("#invoices_pop").removeClass("pop_init ");
        }
        $("#invoices_pop").removeClass("pop_hid").addClass("pop_show");        
      });
      //选中发票
      $("#commitInvoiceTyp").on("tap", function(){
        $("#pop_back").hide();
        var title = $("input[type=radio]:checked").attr("title");
        $("#invoice").html(title);
        $("#invoices_pop").removeClass("pop_show").addClass("pop_hid");          
      });
      //取消选择
      $("#cancelSelectInvoice").on("tap", function(){
        $("#pop_back").hide();
        $("#invoices_pop").removeClass("pop_show").addClass("pop_hid");                
      });      
    },
    //支付配送
    chooseDeliver:function(){
      $("#deliver").on("tap", function(){
        $("#pop_back").show();
        if( $("#deliver_pop").hasClass("pop_init") ){
          $("#deliver_pop").removeClass("pop_init ");
        }
        $("#deliver_pop").removeClass("pop_hid").addClass("pop_show");   
      });
      //取消选择
      $("#cancelSelectDelivery").on("tap", function(){
        $("#pop_back").hide();
        $("#deliver_pop").removeClass("pop_show").addClass("pop_hid"); 
      });
      //确定
      $("#commitDelivery").on("tap", function(){
        $("#pop_back").hide();
        $("#deliver_pop").removeClass("pop_show").addClass("pop_hid"); 
      });
    },
    //提交订单
    commitOrder:function(){
      //提交订单
      $("#commit_order").on("tap", function(){
        redirect("payCenter.html");     
      });
    }
  };

  thisPage = new confirmOrderPage();

});